import 'package:flutter/material.dart';

class AnimatedBuilder2Study extends StatefulWidget {
  const AnimatedBuilder2Study({Key? key}) : super(key: key);

  @override
  _AnimatedBuilder2StudyState createState() => _AnimatedBuilder2StudyState();
}

class _AnimatedBuilder2StudyState extends State<AnimatedBuilder2Study>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation sizeAnimal;

  @override
  void initState() {
    super.initState();
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 20))
          ..repeat(reverse: true);

    sizeAnimal = Tween(begin: 0.0, end: 300)
        .chain(CurveTween(curve: Curves.ease))
        .animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Animation animals1 = Tween(begin: 0.0, end: 1.0)
        .chain(CurveTween(curve: Interval(0.0, 0.2)))
        .animate(_controller);

    Animation animals2 = Tween(begin: 1.0, end: 0.0)
        .chain(CurveTween(curve: Interval(0.4, 0.95)))
        .animate(_controller);

    return Scaffold(
      appBar: AppBar(
        title: Text("自定义动画学习"),
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: _controller,
          builder: (context, child) {
            return Column(
              children: [
                Container(
                  width: 300,
                  height: 300,
                  decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.blue,
                      gradient: RadialGradient(
                          colors: [
                            Colors.blue[500]!,
                            Colors.blue[100]!,
                          ],
                          //渐变开始的位置和结束的位置
                          stops: _controller.value <= 0.2
                              ? [animals1.value, animals1.value + 0.1]
                              : [animals2.value, animals2.value + 0.1])),
                ),
                Container(
                  width: 300,
                  height: 300,
                  decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: Colors.blue,
                      gradient: SweepGradient(
                          colors: [
                            Colors.blue[500]!,
                            Colors.blue[100]!,
                          ],
                          //渐变开始的位置和结束的位置
                          stops: _controller.value <= 0.2
                          ? [animals1.value, animals1.value + 0.1]
                          : [animals2.value, animals2.value + 0.1])),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}
